<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-03-13
 * Time: 16:59:45
 * Info:
 */

namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\common\model\UploadFile as UploadFileModel;
use app\common\model\UploadGroup as UploadGroupModel;
use think\response\Json;

class UploadFile extends Base
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 选择图片的列表
     * @return string
     */
    public function index()
    {
        $type      = input('type', 'more');
        $select_id = input('select_id');
        View::assign('type', $type);
        View::assign('select_id', $select_id);

        return View::fetch();
    }

    /**
     * 分组列表
     * @return \support\Response|\think\response\Json|null
     */
    public function groupList()
    {
        $list0 = [
            'id'        => "0",
            'title'     => '未分组',
            'parent_id' => "0",
            'parentId'  => '0',
            'last'      => true
        ];
        $list  = UploadGroupModel::field("id,parent_id,title")->select()->toArray();
        foreach ($list as $key => $v) {
            $list[$key]['parentId'] = $v['parent_id'];
            $list[$key]['last']     = true;
            unset($v['parent_id']);
        }
        array_unshift($list, $list0);
        $data = [
            'status' => ['code' => 200, 'msg' => '获取成功'],
            'data'   => [
                [
                    'id'       => "-1",
                    'title'    => '全部分组',
                    'parentId' => '0',
                    'last'     => false,
                    'children' => $list
                ],
            ],
        ];

        return json($data);
    }

    /**
     * 添加分组
     * @return Json
     */
    public function groupAdd()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $rule  = [
                'title|分组名称' => 'require'
            ];
            $this->validate($param, $rule);
            $param['group_type'] = 'images';
            UploadGroupModel::create($param);
            $this->success("添加成功");
        }
    }

    /**
     * 编辑分组
     * @return Json
     */
    public function groupEdit()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $rule  = [
                'id|id'          => 'require',
                'title|分组名称' => 'require',
            ];
            $this->validate($param, $rule);
            UploadGroupModel::where(['id' => $param['id']])->data([
                'title'       => $param['title'],
                'update_time' => time()
            ])->update();
            $this->success("修改成功");
        }
    }

    /**
     * 删除分组
     * @return Json
     */
    public function groupDel()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id');
            if (empty($id)) {
                $this->error('参数错误');
            }
            UploadGroupModel::where(['id' => $id])->delete();
            $this->success("删除成功");
        }
    }

    /**
     * 文件库列表
     */
    public function file_list()
    {
        if ($this->request->isAjax()) {
            // 文件列表
            $group_id = $this->request->param('group_id');
            $page     = $this->request->param('page', 1);
            $limit    = $this->request->param('pageSize', 18);
            $first    = intval(($page - 1) * $limit);
            $total    = UploadFileModel::where(function ($query) use ($group_id) {
                if (isset($group_id) && $group_id != '-1') {
                    $query->where('group_id', $group_id);
                }
            })->where('is_delete', 0)->count();
            $list     = UploadFileModel::where(function ($query) use ($group_id) {
                if (isset($group_id) && $group_id != '-1') {
                    $query->where('group_id', $group_id);
                }
            })->where('is_delete', 0)->limit($first, $limit)->order('id desc')->select()->toArray();
            $lastPage = intval(ceil($total / $limit));
            $perPage  = intval($page - 1);

            $file_list = [
                'current_page' => $page,
                'last_page'    => $lastPage,
                'per_page'     => $perPage,
                'total'        => $total,
                'file_list'    => $list
            ];

            $this->success('ok', $file_list);
        }
    }

    /**
     * 删除文件
     */
    public function deleteFiles()
    {
        $fieldIds = input('fileIds');
        if (empty($fieldIds)) {
            $this->error('您没有选择任何文件~');
        }
        UploadFileModel::whereIn('id', $fieldIds)->data(['is_delete' => 1])->update();

        $this->success('删除成功~');
    }

    /**
     * 移动文件至分组
     * @return void
     */
    public function moveFiles()
    {
        $groupId  = input('group_id', 0);
        $fieldIds = input('fileIds');
        if (empty($fieldIds)) {
            $this->error('您没有选择任何文件~');
        }
        UploadFileModel::whereIn('id', $fieldIds)->data(['group_id' => $groupId])->update();

        $this->success('移动分组成功~');
    }

}
